import json

from django.http import JsonResponse
from django.views import View

from app.models import *
from app.url import *


# 访客记录
class UserViewLogs(View):
    def get(self, request):
        try:
            # 用户id
            user_id = request.GET.get('user_id', '')
            # 获取状态
            state = request.GET.get('state', '')
            data = []
            if state == '0':
                print('222')
                viewers = UserViewVisitor.objects.filter(viewers_id=user_id)
                for v in viewers:
                    data.append({
                        'id': v.id,
                        'nickname': v.viewed.userinfo.nickname,
                        'avatar': get_server_url(request) + v.viewed.userinfo.avatar,
                        'gender': v.viewed.userinfo.gender,
                        'age': v.viewed.userinfo.age,
                        'height': v.viewed.userinfo.height,
                        'my_city': v.viewed.userinfo.my_city,
                        'time': v.create_time,
                    })
            else:
                print('111')
                viewed = UserViewVisitor.objects.select_related('viewed').filter(viewed__id=user_id).select_related(
                    'viewed__userinfo')
                for v in viewed:
                    data.append({
                        'id': v.id,
                        'nickname': v.viewers.userinfo.nickname,
                        'avatar': get_server_url(request) + v.viewers.userinfo.avatar,
                        'gender': v.viewers.userinfo.gender,
                        'age': v.viewers.userinfo.age,
                        'height': v.viewers.userinfo.height,
                        'my_city': v.viewers.userinfo.my_city,
                        'time': v.create_time
                    })
            return JsonResponse({
                'code': 200,
                'data': {
                    'result': data
                },
                'msg': '获取成功'
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'data': [],
                'msg': f'获取失败：{str(e)}'
            })

    # 新增记录
    def post(self, request):
        try:
            # 用户id
            print(request.body)
            user_id = json.loads(request.body).get('user_id', '')
            # 被浏览用户的id
            viewed_id = json.loads(request.body).get('viewed_id', '')
            # 创建一条新的记录
            UserViewVisitor.objects.create(id=uuid.uuid4(), viewers_id=user_id, viewed_id=viewed_id)
            return JsonResponse({
                'code': 200,
                'msg': '添加成功'
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'msg': f'添加失败：{str(e)}'
            })


# 获取消息通知
class MessageViewer(View):
    def get(self, request):
        try:
            user_id = request.GET.get('user_id', '')
            print(user_id)
            msg = MessageView.objects.filter(user_id=user_id)
            data = []
            for m in msg:
                data.append({
                    'id': m.id,
                    'content': m.content,
                    'title': m.title,
                    'time': m.time.strftime('%Y-%m-%d %H:%M:%S'),
                    'flag': m.flag
                })
            return JsonResponse({
                'code': 200,
                'msg': '获取成功',
                'data': {
                    'result': data
                }
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'msg': '服务器内部错误' + str(e)
            })

    # 新增信息
    def post(self, request):
        try:
            user_id = request.POST.get('user_id', '')
            content = request.POST.get('content', '')
            title = request.POST.get('title', '')
            MessageView.objects.create(user_id=user_id, content=content, title=title)
            return JsonResponse({
                'code': 200,
                'msg': '信息添加成功'
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'msg': f'信息添加失败：{str(e)}'
            })

    # 更改消息状态
    def put(self, request):
        try:
            id = request.GET.get('id', '')
            MessageView.objects.filter(id=id).update(flag=True)
            return JsonResponse({
                'code': 200,
                'msg': '消息状态更改成功'
            })
        except Exception as e:
            return JsonResponse({
                'code': 500,
                'msg': f'消息状态更改失败：{str(e)}'
            })
